import socket
#import select
import threading
import tkinter as tk
from tkinter import *
from tkinter import scrolledtext
import tkinter.messagebox
import time

##客户端
class App(tk.Tk):
    def __init__(self):
        
        super().__init__()
        
        self.ip=StringVar()
        self.port=StringVar()
        self.note=StringVar()

        self.tkgui()
##界面
    def tkgui(self):
        self.title('socketclinet')
        self.geometry('600x600')
        self.l_ip=tk.Label(self,text='服务器ip:')
        self.l_ip.grid(row=0,column=0,padx=10,pady=10,sticky=tk.E)
        self.e_ip=tk.Entry(self,textvariable=self.ip)
        self.e_ip.grid(row=0,column=1,padx=10,pady=10,sticky=tk.W)
        self.l_port=tk.Label(self,text='服务器端口:')
        self.l_port.grid(row=1,column=0,padx=10,pady=10,sticky=tk.E)
        self.e_port=tk.Entry(self,textvariable=self.port)
        self.e_port.grid(row=1,column=1,padx=10,pady=10,sticky=tk.W)
        
        self.btnst=tk.Button(self,text='连接',command=self.main)
        self.btnst.grid(row=2,column=0,padx=5,pady=5,sticky=tk.E)
        self.btnsp=tk.Button(self,text='断开',command=self.unconnt)
        self.btnsp.grid(row=2,column=1,padx=5,pady=5,sticky=tk.W)
        self.btnsp.configure(state='disable')

        self.sW=80
        self.sH=30
        self.scr=scrolledtext.ScrolledText(self,width=self.sW,height=self.sH,wrap=tk.WORD)
        self.scr.grid(row=3,column=0,columnspan=2)

        self.l_note=tk.Label(self,text='发送内容:')
        self.l_note.grid(row=4,column=0,padx=10,pady=10,sticky=tk.W)
        self.e_note=tk.Entry(self,textvariable=self.note)
        self.e_note.grid(row=4,column=0,padx=10,pady=10,sticky=tk.E)
        self.btnsend=tk.Button(self,text='发送',command=self.sendmg)
        self.btnsend.grid(row=4,column=1,padx=5,pady=5,sticky=tk.W)
                                
##连接
    def connt(self):
        self.host=self.ip.get()
        self.port1=self.port.get()
        if not self.host:
            tkinter.messagebox.showinfo('提示','请输入IP地址！')
            return
        if not self.port1:
            tkinter.messagebox.showinfo('提示','请输入端口！')
            return
        try:
            self.port1=int(self.port1)
        except:
            print('无效端口！')
            tkinter.messagebox.showinfo('提示','无效端口！')
            return

        self.s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
##        self.host='127.0.0.1'
##        self.port=8800
            
        try:
            self.s.connect((self.host,self.port1))
        except:
            print('连接失败')
            self.scr.insert("insert",'连接失败，服务器没有开启或者IP端口错误！\n')
            self.scr.see(tk.END)
            self.scr.update()
            tkinter.messagebox.showinfo('提示','连接失败！')
            return

        addr=self.s.getsockname()    
        self.scr.insert("insert",'yourclient:'+str(addr[1])+'\n')
        self.scr.see(tk.END)
        self.scr.update()
        self.inputs=[self.s]
            
        self.btnst.configure(state='disable')
        self.btnsp.configure(state='normal')        

##断开
    def unconnt(self):
        self.s.close()
        self.scr.insert("insert",'已断开\n')
        self.scr.see(tk.END)
        self.scr.update()
        self.btnst.configure(state='normal')
        self.btnsp.configure(state='disable')
        
    def sendmg(self):
        try:
            sendmsg=self.note.get()
            if not sendmsg:
                tkinter.messagebox.showinfo('提示','请输入发送内容！')
                return
            self.s.sendall(bytes(sendmsg,encoding='utf-8'))
        except:
            print('没有连接')
            tkinter.messagebox.showinfo('提示','没有连接！')
            return
##接收       
    def receiv(self):
        while True:
            #stdinput, stdoutput, stderr = select.select(self.inputs, [], [])
           
            try:
                acceptmsg=str(self.s.recv(1024),encoding='utf-8')
                print(''.join(acceptmsg))
                if not acceptmsg:
                    print('服务器断开')
                    self.scr.insert("insert",'服务器断开\n')
                    self.scr.see(tk.END)
                    self.scr.update()
                    self.btnst.configure(state='normal')
                    self.btnsp.configure(state='disable')
                    break
                self.scr.insert("insert",acceptmsg+'\n')
                self.scr.see(tk.END)
                self.scr.update()
            except:
                print('与服务器没有连接')
                tkinter.messagebox.showinfo('提示','没有连接！')
                break
##开始            
    def main(self):
        self.connt()
        ts=threading.Thread(target=self.receiv)
        #ts.setDaemon(True) 
        ts.start()
        #ts.join()

if __name__=='__main__':

    app=App()
    app.mainloop()

